package com.bwie.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.MenuMapper;
import com.bwie.pojo.Menu;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.mapper.UserMapper;
import com.bwie.utils.R;
import com.bwie.vo.LoginVo;
import com.bwie.vo.MenuVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author 陶仕豪
* @description 针对表【tb_user(用户表)】的数据库操作Service实现
* @createDate 2025-07-31 09:17:46
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    UserMapper userMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    MenuMapper mapper;
    @Override
    public R login(LoginVo loginVo) {
        User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserMobile, loginVo.getKeyWord()).or().eq(User::getUserEmail, loginVo.getKeyWord()));
        if(user==null){
            return R.ERROR(500,"用户名或邮箱不存在");
        }
        if(!user.getUserPass().equals(loginVo.getUserPass())){
            return R.ERROR(500,"密码错误");
        }
        String key="token-"+user.getUserId();
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", user.getUserId())
                .claim("userName", user.getUserName())
                .compact();

        stringRedisTemplate.opsForValue().set(key,token,60, TimeUnit.MINUTES);
        user.setToken(token);
        user.setUserPass(null);
        return R.OK(user);
    }

    @Override
    public R getUserByEmail(String email) {
        User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserEmail, email));
        if(user==null){
            return R.ERROR(500,"用户不存在");
        }
        return R.OK(user);
    }

    @Override
    public R getMenuList(MenuVo menuVo) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(menuVo.getMenuName())){
            wrapper.lambda().like(Menu::getMenuName,menuVo.getMenuName());
        }
        //查询菜单
        List<Menu> menus = mapper.selectList(wrapper);
        List<Tree<String>> treeList = TreeUtil.build(menus, "0", (tree, menu) -> {
            menu.setId(tree.getMenuId()+"");
            menu.setName(tree.getMenuName());
            menu.setParentId(tree.getMenuPid()+"");

            menu.putExtra("path",tree.getMenuPath());
            menu.putExtra("createTime",tree.getCreateTime());
        });

        return R.OK(treeList);
    }
}




